from django.shortcuts import render

# Create your views here.

from .models import CourseCategory,Course,CourseChapter
from .serializer import CourseCategorySerialer,CourseSerializer,CourseChapterSerializer
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
# 过滤组件：搜索功能、排序功能
from rest_framework.filters import SearchFilter, OrderingFilter
# from django_filters.rest_framework import DjangoFilterBackend
from .pagination import BasicPagination
from .SerachBackend import SearchByName

from django_filters.rest_framework import DjangoFilterBackend
class CategoryView(GenericViewSet, ListModelMixin):
    queryset = CourseCategory.objects.filter(is_delete=False, is_show=True).order_by('-orders')
    serializer_class = CourseCategorySerialer



from rest_framework.mixins import RetrieveModelMixin
from .SerachBackend import SearchByPrice
from .SerachBackend import CourseFilterSet
class CourseView(GenericViewSet, ListModelMixin,RetrieveModelMixin):
    queryset = Course.objects.filter(is_delete=False, is_show=True).order_by('-orders')
    serializer_class =CourseSerializer
    # 价格，人气，升序和降序
    # filter_backends = [OrderingFilter,DjangoFilterBackend,SearchByName]

    # 它俩一起用
    filter_backends = [DjangoFilterBackend]
    filter_class = CourseFilterSet
    # filter_backends = [OrderingFilter,DjangoFilterBackend,SearchByPrice]
    # filter_backends = [OrderingFilter,DjangoFilterBackend,SearchFilter]
    # 允许排序的字段
    # ordering_fields = ['id', 'price', 'students']
    # # 允许过滤的字段
    # filter_fields = ['course_category']

    # filter_backends=[DjangoFilterBackend,]
    # filter_fields = ['course_category']
    # 允许搜索（内置搜索，只能按照search='name或者id或者brief'）
    # 想实现更高级的搜索，需要自己写
    # 基于django-filter扩写
    # 纯自己写
        #继承它GenericAPIView可以配置它filter_backends=自己定义的搜索类，重写filter_queryset方法，就能完成搜索

    # search_fields = ['name', 'id', 'brief']

    pagination_class =BasicPagination


## 如果这个接口支持搜索功能


# 课程章节接口
# 查多条，list
class ChapterViewSet(GenericViewSet,ListModelMixin):
    queryset = CourseChapter.objects.filter(is_show=True,is_delete=False).order_by('orders')
    serializer_class = CourseChapterSerializer

    # 加上过滤功能，按课程的id号来过滤
    filter_backends = [DjangoFilterBackend]
    filter_fields = ['course']


from rest_framework.filters import SearchFilter
class CourserSearchView(GenericViewSet,ListModelMixin):
    queryset = Course.objects.filter(is_delete=False, is_show=True).order_by('-orders')
    serializer_class =CourseSerializer
    # 内置搜索
    filter_backends = [SearchFilter]
    search_fields = ['name', 'brief']
    pagination_class = BasicPagination

    # def list(self, request, *args, **kwargs):
    #     res=super().list(request, *args, **kwargs)
    #     # 实战课数据
    #     res.data
    #
    #     # 免费课数据
    #     data2
    #
    #     # 轻课
    #     data3
    #     return  APIRespone(lite_course=data3,)